<template>
	<view id="index">
		<view class="title">
			<input type="text" v-model='title' placeholder="请输入标题" />
		</view>
		<view class="abstract">
			<input type="text" v-model='abstract' placeholder="请输入摘要" />
		</view>
		<view class="file">
			<video :src="src"></video>
			<button class="choose" @tap="choose">选择视频</button>
		</view>
		<view class="file">
			<image class="img" :src="img[0]"></image>
			<button class="choose" @tap="choose1">选择封面</button>
		</view>
		<view class="up">
			<button class="upload" @tap="uploadvideo">发布</button>
		</view>
	</view>
</template>

<script>
	import con from 'pages/const.js'
	export default {
		data() {
			return {
				src: '',//临时地址
				img:'',//临时地址
				img1:'',
				src1:'',
				title: '',
				abstract: ''
			}
		},
		onLoad() {
			if(!this.$store.state.user){
				uni.redirectTo({
					url: '/pages/user/login'
				})
				uni.showToast({
					'title': '请登录'
				});
			}
		},
		methods: {
			choose() {
				var self = this;
				uni.chooseVideo({
					count: 1,
					compressed:false,
					sourceType: ['camera', 'album'],
					success: function(res) {
						self.src = res.tempFilePath;
					}
				});
			},
			choose1() {
				var self = this;
				uni.chooseImage({
				    count: 1,
				    sizeType: ['compressed'],
				    sourceType: ['original'],
				    success: function (res) {
				        self.img = res.tempFilePaths
				    }
				});
			},
			uploadvideo(){
				if(this.src===''){
					uni.showToast({
						'title': '请选择视频文件'
					});
					return
				}
				if(this.img===''){
					uni.showToast({
						'title': '请选择封面'
					});
					return
				}
				if(this.title===''||this.abstract===''){
					uni.showToast({
						'title': '标题简介不能为空'
					});
					return
				}
				uni.showLoading({
				    title: '上传封面中'
				});
				var t=this
				uni.uploadFile({
				    url: con.request_ip+'/resource/upload/image',
				    filePath: t.img[0],
				    name: 'image',
				    success: function(res){
						var data=JSON.parse(res.data)
				        if(data.code===200){
							t.img1=data.msg
							t.upload_url(t)
						}
						else
							uni.showToast({
								'title': data.msg,
							});
				    },
					fail:function(){
						uni.showToast({
						    'title': '请求发起失败',
						});
					},
					complete:function(){
						uni.hideLoading();
					}
				});
			},
			upload_url(t){
				uni.showLoading({
				    title: '上传视频中'
				});
				uni.uploadFile({
				    url: con.request_ip+'/resource/upload/video',
				    filePath: t.src,
				    name: 'video',
				    success: function(res){
						var data=JSON.parse(res.data)
				        if(data.code===200){
							t.src1=data.msg
							t.upload(t)
						}
						else
							uni.showToast({
								'title': data.msg,
							});
				    },
					fail:function(){
						uni.showToast({
						    'title': '请求发起失败',
						});
					},
					complete:function(){
						uni.hideLoading();
					}
				});
			},
			upload(t) {
				if(t.src1===''||t.img1===''){
					uni.hideLoading();
					return
				}
				uni.showLoading({
				    title: '上传数据中'
				});
				uni.request({
					url : con.request_ip+'/resource/upload_sp',
					method: 'POST',
					sslVerify: false,
					data: {
						'title': t.title,
						'abstract': t.abstract,
						'url': t.src1,
						'fm': t.img1
					},
					success:function(res) {
						//console.log(res)
						uni.showToast({
							'title': res.data.msg,
						});
						if(res.data.code===200)
							uni.switchTab({
								url:'/pages/page/article_index'
							})
					},
					fail: (res) => {
						uni.showToast({
						    'title': '请求发起失败',
						});
					},
					complete:function(){
						uni.hideLoading();
					}
				})
			}
		}
	}
</script>

<style scoped>
	#index {
		color: #514f4f;
		width: 100vw;
		height: 100vh;
		position: relative;
		top: var(--status-bar-height);
	}

	.title {
		height: 64rpx;
		width: 650rpx;
		margin: 14rpx 50rpx 24rpx 50rpx;
		padding: 28rpx 0 34rpx 0;
	}
	.title input {
		font-size: 46rpx;
	}
	.abstract {
		height: 64rpx;
		width: 650rpx;
		padding: 18rpx 0 24rpx 0;
		margin: 6rpx 50rpx 24rpx 50rpx;
		font-size: 46rpx;
	}
	.abstract input {
		font-size: 36rpx;
	}
	input {
		border-width: 0 0 1px 0;
		border-style: solid;
	}
	input:hover {
		border-color: #00aaff;
		border-width: 0 0 2px 0;
	}

	.file {
		margin: 8rpx 50rpx 34rpx 50rpx;
	}

	button {
		width: 80%;
		height: 100%;
		text-align: center;
		font-size: 32rpx;
		background: #2db7f5;
		border-radius: 40rpx;
		color: white;
		top: 26rpx;
	}
	.up {margin: 46rpx 50rpx 24rpx 50rpx;}
	.upload {width: 100%;}
</style>
